clear all;

%% construct data
% for baseline results
cd ..; cd ..; cd ..; 
load('input.mat','Pi'); total_size=sum(Pi);nmkttotal=length(Pi);
load('code_main_model\6_implied_w_r\computation_results\data.mat','frac_credit','frac_entre','labor_demand_stat','income_entre','income_worker','grid_income','period_trans','nmkt');
load('code_main_model\6_implied_w_r\input\comm_loc_model.mat','comm_loc_model');
cd 'code_figure_table\OA\Table OA12';

income_dist=income_entre+income_worker;
income_treat(1:period_trans-1,1:nmkt)=nan;
for t=1:period_trans-1
    for i=1:nmkt
        income_treat(t,i)=dot(squeeze(income_dist(t,i,:)),grid_income)/sum(squeeze(income_dist(t,i,:)));
    end
end
mktsize_treat=sum(income_dist,3);
frac_credit_treat=frac_credit./mktsize_treat;
frac_entre_treat=frac_entre./mktsize_treat;
employment_treat=labor_demand_stat./mktsize_treat;

% for counterfactual simulations
cd ..; cd ..; cd ..; 
load('code_main_model\7_simulate_perfect_DID\1_perfect_match\computation_results\data.mat','frac_credit','frac_entre','labor_demand_stat','income_entre','income_worker','grid_income','period_trans','nmkt');
% load('code_main_model\7_simulate_perfect_DID\2_anticipation\computation_results\data.mat','frac_credit','frac_entre','labor_demand_stat','income_entre','income_worker','grid_income','period_trans','nmkt');
% load('code_main_model\7_simulate_perfect_DID\3_lag\computation_results\data.mat','frac_credit','frac_entre','labor_demand_stat','income_entre','income_worker','grid_income','period_trans','nmkt');
cd 'code_figure_table\OA\Table OA12';

income_dist=income_entre+income_worker;
income_control(1:period_trans-1,1:nmkt)=nan;
for t=1:period_trans-1
    for i=1:nmkt
        income_control(t,i)=dot(squeeze(income_dist(t,i,:)),grid_income)/sum(squeeze(income_dist(t,i,:)));
    end
end
mktsize_control=sum(income_dist,3);
frac_credit_control=frac_credit./mktsize_control;
frac_entre_control=frac_entre./mktsize_control;
employment_control=labor_demand_stat./mktsize_control;

%% organize data
% for income_control, the column will be nan if the market is not a
% treatment market
mkt=find(~isnan(income_control(1,:)));
nmkt=length(mkt);
year=[1986, 1988, 1990, 1992, 1994, 1996];
nyear=length(year);
n=nmkt*nyear;
treat(1:n,1:6)=0;
control(1:n,1:6)=0;

for i_mkt=1:nmkt
    index=(i_mkt-1)*nyear+1:i_mkt*nyear;
    treat(index,1)=year;
    treat(index,2)=mkt(i_mkt);
    treat(index,3)=log(income_treat(1:2:11,mkt(i_mkt))); % income per capita
    treat(index,4)=log(employment_treat(1:2:11,mkt(i_mkt)).*mktsize_treat(1:2:11,mkt(i_mkt))*total_size);
    treat(index,5)=frac_entre_treat(1:2:11,mkt(i_mkt));
    treat(index,6)=frac_credit_treat(1:2:11,mkt(i_mkt));

    control(index,1)=year;
    control(index,2)=mkt(i_mkt);
    control(index,3)=log(income_control(1:2:11,mkt(i_mkt))); % income per capita
    control(index,4)=log(employment_control(1:2:11,mkt(i_mkt)).*mktsize_control(1:2:11,mkt(i_mkt))*total_size);
    control(index,5)=frac_entre_control(1:2:11,mkt(i_mkt));
    control(index,6)=frac_credit_control(1:2:11,mkt(i_mkt));
end

%% add year when bank opens
treat=[treat(:,1:2),nan(size(treat,1),2),treat(:,3:end)];
control=[control(:,1:2),nan(size(control,1),2),control(:,3:end)];

for i_mkt=1:nmkt 
    index=find(treat(:,2)==mkt(i_mkt));
    
    branch_year=find(comm_loc_model(mkt(i_mkt),:),1,'first');
    treat(index,3)=1984+branch_year;
end
treat(:,4)=treat(:,3)-treat(:,1);

%% add treated dummy and interactions
treated=ones(n,1);

p_m3=0; p_m3(1:n,1)=0;
p_m3(treat(:,4)>=4)=1;
p_m2=0; p_m2(1:n,1)=0;
p_m2(treat(:,4)==2 | treat(:,4)==3)=1;
p_m1=0; p_m1(1:n,1)=0;
p_m1(treat(:,4)==0 | treat(:,4)==1)=1;
p_0=0; p_0(1:n,1)=0;
p_0(treat(:,4)==-2 | treat(:,4)==-1)=1;
p_p1=0; p_p1(1:n,1)=0;
p_p1(treat(:,4)==-4 | treat(:,4)==-3)=1;
p_p2=0; p_p2(1:n,1)=0;
p_p2(treat(:,4)<=-5)=1;

p_m3xt=p_m3.*treated;
p_m2xt=p_m2.*treated;
p_m1xt=p_m1.*treated;
p_0xt=p_0.*treated;
p_p1xt=p_p1.*treated;
p_p2xt=p_p2.*treated;

treat=[treat, treated, p_m3, p_m2, p_m1, p_0, p_p1, p_p2, p_m3xt, p_m2xt, p_m1xt, p_0xt, p_p1xt, p_p2xt];
control=[control, zeros(n,1), p_m3, p_m2, p_m1, p_0, p_p1, p_p2, zeros(n,6)];

% distinguish control markets
control(:,2)=control(:,2)+nmkttotal;

market_y=[treat;control];


%% save

filename = 'panel_all.xlsx';
market_y=array2table(market_y,'VariableNames',{'year','fixedeffectdummy','eventyear_index','gapyear_index','incpc','num_worker','entre','credit','treated','p_m3','p_m2','p_m1','p_0','p_p1','p_p2','p_m3xt','p_m2xt','p_m1xt','p_0xt','p_p1xt','p_p2xt'});
writetable(market_y,filename,'Sheet','Sheet1','WriteVariableNames',true);
